package auction.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

public class ReceiverThread extends Thread {
	
	private BufferedReader in;

	private boolean shutdown = false;

	public ReceiverThread(Socket socket) throws IOException {
		
		this.in = new BufferedReader(new InputStreamReader(socket
				.getInputStream()));
	}

	public void run() {
		String input;

		try {
			while ((input = in.readLine()) != null) {
				
				
				if (input.startsWith("!list")) {
					/* server answer to !list */					
					Client.printAuctionList(input.substring(5));
				} 
				else if (input.equals("break"))
					Client.cleanUp(0);
				else
					System.out.println(input);	//regular server answer
			}
			
		Client.cleanUp(0);
			
		} catch (IOException e) {
			if (!shutdown) {
				e.printStackTrace();
				cleanUp();
			}
		}
	}

	public void cleanUp() {
		shutdown = true;
	}
}
